/// 用户参与的活动模型
class UserParticipatedActivity {
  final int activityId;
  final String activityTitle;
  final List<String> activityImages;
  final List<String> activityType;
  final List<String> activityTags;
  final String activityDetails;
  final String activityLocation;
  final double? longitude;
  final double? latitude;
  final String activityPublishTime;
  final List<String> activityCategory;
  final List<String> activityRestriction;
  final int activityCurrentParticipants;
  final int activityMaxParticipants;
  final int publisherId;
  final String publisherAvatar;
  final String publisherNickname;
  final String publisherSignature;
  final List<dynamic> publisherTags;
  final List<String> activityStatus;
  final List<String> status;
  final Map<String, dynamic>? extraData;
  final String joinTime;
  final String participantStatus;
  final String activityStatusMessage;

  UserParticipatedActivity({
    required this.activityId,
    required this.activityTitle,
    required this.activityImages,
    required this.activityType,
    required this.activityTags,
    required this.activityDetails,
    required this.activityLocation,
    this.longitude,
    this.latitude,
    required this.activityPublishTime,
    required this.activityCategory,
    required this.activityRestriction,
    required this.activityCurrentParticipants,
    required this.activityMaxParticipants,
    required this.publisherId,
    required this.publisherAvatar,
    required this.publisherNickname,
    required this.publisherSignature,
    required this.publisherTags,
    required this.activityStatus,
    required this.status,
    this.extraData,
    required this.joinTime,
    required this.participantStatus,
    required this.activityStatusMessage,
  });

  factory UserParticipatedActivity.fromJson(Map<String, dynamic> json) {
    return UserParticipatedActivity(
      activityId: json['activityId'] ?? 0,
      activityTitle: json['activityTitle'] ?? '',
      activityImages: _parseStringList(json['activityImages']),
      activityType: _parseStringList(json['activityType']),
      activityTags: _parseStringList(json['activityTags']),
      activityDetails: json['activityDetails'] ?? '',
      activityLocation: json['activityLocation'] ?? '',
      longitude: json['longitude']?.toDouble(),
      latitude: json['latitude']?.toDouble(),
      activityPublishTime: json['activityPublishTime'] ?? '',
      activityCategory: _parseStringList(json['activityCategory']),
      activityRestriction: _parseStringList(json['activityRestriction']),
      activityCurrentParticipants: json['activityCurrentParticipants'] ?? 0,
      activityMaxParticipants: json['activityMaxParticipants'] ?? 0,
      publisherId: json['publisherId'] ?? 0,
      publisherAvatar: json['publisherAvatar'] ?? '',
      publisherNickname: json['publisherNickname'] ?? '',
      publisherSignature: json['publisherSignature'] ?? '',
      publisherTags: _parseDynamicList(json['publisherTags']),
      activityStatus: _parseStringList(json['activityStatus']),
      status: _parseStringList(json['status']),
      extraData: json['extraData'] is Map ? Map<String, dynamic>.from(json['extraData']) : null,
      joinTime: json['joinTime'] ?? '',
      participantStatus: _parseParticipantStatus(json['participantStatus']),
      activityStatusMessage: json['activityStatusMessage'] ?? '',
    );
  }

  /// 解析字符串列表，处理各种数据格式（包括null值）
  static List<String> _parseStringList(dynamic data) {
    if (data == null) return [];
    
    try {
      if (data is List) {
        // 过滤掉null值，并将其他值转换为字符串
        return data
            .where((item) => item != null)  // 过滤null值
            .map((item) => item.toString())
            .where((str) => str.isNotEmpty && str != 'null')  // 过滤空字符串和'null'字符串
            .toList();
      } else if (data is String && data.isNotEmpty && data != 'null') {
        return [data];
      }
    } catch (e) {
      print('解析字符串列表出错: $e, data: $data');
    }
    
    return [];
  }

  /// 解析动态类型列表，处理各种数据格式（包括null值）
  static List<dynamic> _parseDynamicList(dynamic data) {
    if (data == null) return [];
    
    try {
      if (data is List) {
        // 过滤掉null值
        return data.where((item) => item != null).toList();
      } else if (data != null) {
        return [data];
      }
    } catch (e) {
      print('解析动态列表出错: $e, data: $data');
    }
    
    return [];
  }

  /// 解析参与者状态，处理数组或字符串格式
  static String _parseParticipantStatus(dynamic data) {
    if (data == null) return '';
    
    try {
      if (data is List && data.isNotEmpty) {
        // 如果是数组，取第一个非空元素
        for (var item in data) {
          if (item != null && item.toString().isNotEmpty) {
            return item.toString();
          }
        }
      } else if (data is String) {
        return data;
      } else if (data != null) {
        return data.toString();
      }
    } catch (e) {
      print('解析参与者状态出错: $e, data: $data');
    }
    
    return '';
  }

  Map<String, dynamic> toJson() {
    return {
      'activityId': activityId,
      'activityTitle': activityTitle,
      'activityImages': activityImages,
      'activityType': activityType,
      'activityTags': activityTags,
      'activityDetails': activityDetails,
      'activityLocation': activityLocation,
      'longitude': longitude,
      'latitude': latitude,
      'activityPublishTime': activityPublishTime,
      'activityCategory': activityCategory,
      'activityRestriction': activityRestriction,
      'activityCurrentParticipants': activityCurrentParticipants,
      'activityMaxParticipants': activityMaxParticipants,
      'publisherId': publisherId,
      'publisherAvatar': publisherAvatar,
      'publisherNickname': publisherNickname,
      'publisherSignature': publisherSignature,
      'publisherTags': publisherTags,
      'activityStatus': activityStatus,
      'status': status,
      'extraData': extraData,
      'joinTime': joinTime,
      'participantStatus': participantStatus,
      'activityStatusMessage': activityStatusMessage,
    };
  }

  /// 获取支付方式类型文本
  String get paymentTypeText {
    if (activityType.isEmpty) return 'AA';
    return activityType.first;
  }

  /// 获取主要图片URL
  String get primaryImageUrl {
    if (activityImages.isEmpty) {
      return 'https://bai-ying.oss-cn-qingdao.aliyuncs.com/image/user_test/user_16.webp'; // 默认图片
    }
    return activityImages.first;
  }
}
